<?php
class InrecordsController extends AppController {

    public $helpers = array('Html', 'Form');
    public $components = array('Session');

    public function index() {
        $pageTitle ="GHS Registry: In-coming Correspondence";
        $in_record_data = $this->Inrecord->find('all');
        $this->set(compact('in_record_data','pageTitle'));
    }

    
    public function add(){
    $pageTitle ="GHS Registry: In-coming Correspondence";
     if($_POST){
       // pr($_POST);
        $data['Inrecord'] = array(
            'ddate' => $_POST['ddate'],
            'title' => $_POST['title'],
            'reference' => $_POST['reference'],
            'staffid' => $_POST['staffid'],
            'staffname' => $_POST['staffname'],
            'region' => $_POST['region'],
            'facility' => $_POST['facility'],
            'endorsed' => $_POST['endorsed'],
            'recipient' => $_POST['recipient'],
            'forwarded' => $_POST['forwarded'],
            'datereceived' => $_POST['datereceived'],
            'processed' => $_POST['processed'],
            'fdate' => $_POST['fdate'],
            'refereddate' => $_POST['refereddate'],
            'comments' => $_POST['comments']
            );

        if(!$this->Inrecord->save($data)){
            $this->Session->setFlash('Error Saving Data');
        }else{
             $this->Session->setFlash('Data Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','errorMsg'));
}
    
    // A functin to delete Records
    public function delete($id = null)
    {
       if (!$id) 
       {
        $this->Session->setFlash('Invalid id for Record');
        $this->redirect(array('action' => 'index'));
    }
    if ($this->Inrecord->delete($id)) 
    {
        $this->Session->setFlash('Record Deleted Please');
    } else 
    {
        $this->Session->setFlash(__('Record Not Deleted', true));
    }
    $this->redirect(array('action' => 'index'));
}

    // A functin to edit Records
    function edit($id = null){
    $record_id = $id;
    $conditions = array('Inrecord.id' => $record_id);
    $record = $this->Inrecord->find('first', array('conditions' => $conditions, 'recursive' => 1));
   // pr($record);
            
     $pageTitle ="Incoming Correspondence - Edit Records";
     if($_POST){
        //pr($_POST);
        $data['Inrecord'] = array(
            'id' => $_POST['record_id'],
            'ddate' => $_POST['ddate'],
            'title' => $_POST['title'],
            'reference' => $_POST['reference'],
            'staffid' => $_POST['staffid'],
            'staffname' => $_POST['staffname'],
            'region' => $_POST['region'],
            'facility' => $_POST['facility'],
            'endorsed' => $_POST['endorsed'],
            'recipient' => $_POST['recipient'],
            'forwarded' => $_POST['forwarded'],
            'datereceived' => $_POST['datereceived'],
            'processed' => $_POST['processed'],
            'datedelivered' => $_POST['datedelivered'],
            'refereddate' => $_POST['refereddate'],
            'comments' => $_POST['comments']
            );

        if(!$this->Inrecord->save($data)){
            $this->Session->setFlash('Error Saving Data');
        }else{
             $this->Session->setFlash('Data Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','record'));
}

     // A functin to view Records
    public function view($id) {
    if (!$id) {
        throw new NotFoundException(__('Invalid Record'));
    }

    $inrecord = $this->Inrecord->findById($id);
    if (!$inrecord) {
        throw new NotFoundException(__('Invalid Record'));
    }
    $this->set('inrecord', $inrecord);
}

function report($id = null){
    $record_id = $id;
    $conditions = array('Inrecord.id' => $record_id);
    $record = $this->Inrecord->find('first', array('conditions' => $conditions, 'recursive' => 1));
//    pr($record);

}

}
?>
